package createThread;

/**
 * @program: javase
 * @description: 继承Thread创建子线程
 * @author: lld
 * @create: 2021-03-13 16:21
 **/
public class Thread1 {
    public static void main(String[] args) throws InterruptedException {
        MyThread t = new MyThread();

        t.start();//子线程,
        // 1.必须调用start方法，如果调用run方法，只是方法调用属于主线程
//        t.start();//2.线程不能多次调用，报java.lang.IllegalThreadStateException
        //主线程
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
//                if(i==10){
//                    t.join();
//                }
                System.out.println(i + Thread.currentThread().getName());
            }

        }
    }

}

class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 != 0) {
//                if(i==15){
//                    yield();
//                }
                try {
                    sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Thread.currentThread().setName("createThread.MyThread");
                System.out.println(i + Thread.currentThread().getName());
            }
        }
    }
}
